package servlet;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import connection.DbConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.StringTokenizer;

import model.EmailingSystem;
import model.PollUserImpl;
import bean.PollUser;

public class changePasswordServlet extends HttpServlet {
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -3021726024222994772L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost (request, response);
	}
	
	public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("GBK");
		
		System.out.println("changePasswordServlet run");
		
		// Clear other success messages
		request.getSession().setAttribute("settings_change_personal_success", "f");
		request.getSession().setAttribute("settings_add_device_success","f");
		request.getSession().setAttribute("settings_delete_device_success","f");
		
		// Get parameters
		String currentPassword = request.getParameter("current_pw");
		String newPassword = request.getParameter("new_pw");
		String repeatPassword = request.getParameter("repeat_pw");

		// Error checking
		System.out.println("current password: "+currentPassword);
		System.out.println("new password: "+newPassword);
		System.out.println("repeat password: "+repeatPassword);

		// Create user bean
		PollUser user = new PollUser();
		user.setUsername(request.getSession().getAttribute("username").toString());
		System.out.println("Username: "+user.getUsername());
		
		// Set all other attributes from database
		PollUserImpl.setUserAttributes(user);
		
		// Check for incorrect current password
		if (!currentPassword.equals(user.getPassword())) {
			request.getSession().setAttribute("settings_incorrect_pw", "t");
			request.getSession().setAttribute("settings_success", "f");
		} else {
			request.getSession().setAttribute("settings_incorrect_pw", "f");
			
			// Change user bean password
			user.setPassword(newPassword);
			
			// Update database
			DbConnection db = new DbConnection();
			String sql = "Update PollUser set password = '"+user.getPassword()+"' where username = '"+user.getUsername()+"'";
			db.doUpdate(sql);
			
			// Send change of password email
			EmailingSystem.sendPasswordChange(user);
			
			// Change session variables
			request.getSession().setAttribute("password", user.getPassword());
			
			// Set session attribute to success
			request.getSession().setAttribute("settings_success", "t");
			
		}
		
		// Send back home
		response.sendRedirect("settings.jsp");
	}

}
